#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using TVC.Layout;

#endregion

namespace TVC.Modules.CustomControl
{
    public partial class LookupBox : UserControl
    {
        public GridLayout Layout { get; set; }
        public MainForm MainForm { get; set; }
        string _lookup;
        private bool _nonValidated = false;

        [PropertyTab]
        public bool NonValidated
        {
            get { return _nonValidated; }
            set { _nonValidated = value; }
        }

        public string Lookup
        {
            get { return _lookup; }
            set
            {
                if (value != null && _lookup != value)
                {
                    _lookup = value;
                    Layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", String.Format("{0}Documents\\Layout\\{1}\\", MainForm.ServerPath, MainForm.PODInfo.USER_ID), _lookup);
                }
            }
        }
        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);
        }



        public override bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set
            {
                btSearch.Enabled = cb.Enabled = base.Enabled = value;

            }
        }
        public object Value
        {
            get
            {
                if (_nonValidated) return cb.Text;
                return string.Format("{0}", cb.SelectedValue);
            }
            set
            {
                if (!_nonValidated) cb.SelectedValue = string.Format("{0}", value);
                else cb.Text = string.Format("{0}", value);
                // cb.SelectedValue = string.Format("{0}", value);
            }
        }
        public object SelectedItem { get { return cb.SelectedItem; } }

        public object DataSource { get { return cb.DataSource; } set { cb.DataSource = value; } }
        private void OpenView()
        {
            if (frm == null || frm.IsDisposed || !frm.Visible)
                Find();
            else if (!frm.Visible)
                frm.Visible = true;
            //frm.ShowDialog();
        }
        public LookupBox()
        {
            InitializeComponent();
            cb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cb.SelectedValueChanged += new EventHandler(cb_SelectedValueChanged);
            cb.GotFocus += new EventHandler(cb_GotFocus);
            //cb.Leave += new EventHandler(cb_Leave);
            cb.SelectionChangeCommitted += new EventHandler(cb_SelectedValueChanged);
            //cb.Validated += new EventHandler(cb_Validated);
            // cb.TextChanged += new EventHandler(cb_Validated);
        }

        void cb_Validated(object sender, EventArgs e)
        {
            if (ComboBoxValidated != null) ComboBoxValidated(sender, e);
        }

        void cb_GotFocus(object sender, EventArgs e)
        {
            this.Focus();
        }



        public override string Text
        {
            get
            {
                return cb.Text;
            }
            set { cb.Text = value; }
        }



        void cb_SelectedValueChanged(object sender, EventArgs e)
        {
            if (SelectedValueChanged != null) SelectedValueChanged(this, e);
        }
        public event EventHandler SelectedValueChanged;
        public event EventHandler ComboBoxValidated;
        private DataTable GetDataTable()
        {

            object datasource = cb.DataSource as BindingSource;
            if (datasource == null)
            {
                datasource = cb.DataSource;
            }
            else
            {
                datasource = ((BindingSource)cb.DataSource).DataSource;
            }
            DataTable dt = null;
            if (datasource is DataTable)
                dt = datasource as DataTable;
            else if (datasource is DataView)
            {
                DataView dv = datasource as DataView;
                dt = dv.Table;
            }
            // dt.DefaultView.RowFilter = "";
            //if (code != null && code != "") dt.DefaultView.RowFilter = ValueMember + " LIKE '" + code + "%'";
            return dt;
        }
        public bool ReadOnly { get { return !cb.Enabled; } set { cb.Enabled = btSearch.Enabled = !value; } }
        Forms.frmLKView frm;
        public string ValueMember { get { return cb.ValueMember; } set { cb.ValueMember = value; } }
        public string DisplayMember { get { return cb.DisplayMember; } set { cb.DisplayMember = value; } }
        public void Find()
        {

            frm = new Forms.frmLKView(Layout, MainForm);

            DataTable dt = GetDataTable();

            if (cb.Text != null && cb.Text != "")
            {
                frm.FilterRow = ValueMember + " LIKE '" + cb.Text + "%'";
            }
            frm.DataSource = dt.Copy();
            frm.FormClosed += (form, args) =>
            {

                if (((Forms.frmLKView)form).DialogResult == DialogResult.OK)
                {

                    if (ValueMember != null && ((Forms.frmLKView)form).ReturnValue.Table.Columns.Contains(ValueMember))
                    {
                        cb.SelectedValue = ((Forms.frmLKView)form).ReturnValue[ValueMember].ToString();

                        //if (SelectionChangeCommitted != null) SelectionChangeCommitted(this, args);
                    }
                }

                cb.Focus();
            };
            frm.ShowDialog();

        }
        public void SetDropDownWith(int w)
        {
            cb.DropDownWidth = w;
        }
        private void bt_Click(object sender, EventArgs e)
        {
            OpenView();
        }
    }
}